/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2012-10-24         luwei
 */

package com.tianci.appstore.ui.widget;

import android.content.Context;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.widget.RelativeLayout;

import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.ui.data.SkyCategoryData;
import com.tianci.appstore.ui.widget.SkyCategoryName.Category;

import java.util.ArrayList;
import java.util.List;

public class SkyCategoryBar extends RelativeLayout implements
        OnKeyListener, OnFocusChangeListener {
    protected Context mContext = null;
    protected int mClickedPos = 0;
    protected int mCurrentPos = 0;
    protected SkyCategoryName mCategoryName = null;
    protected SkyCategoryCursor mCategoryCursor = null;
    protected List<SkyCategoryData> mCategoryDatas = null;
    private OnCategoryChangeListener mCategoryChangeListener = null;
    private OnCategoryClickListener mCategoryClickListener = null;
    private CustomFocusCallBack mFocusCallBack = null;
    private int endX = 0;// JHF
    private int endY = 0;// JHF
    private int page = -1;// JHF
    private boolean mFocus = false;
    private boolean mKey = true;
    private static String mCurrentName = "";

    public interface CustomFocusCallBack {
        public boolean setFocus(int keyCode);
    }

    public interface OnCategoryChangeListener {
        public void categoryChanged(SkyCategoryData data, int keyCode);
    }

    public interface OnCategoryClickListener {
        public void categoryClicked(SkyCategoryData data, int cgyIndex);
    }

    public void setCustomFocusCallBack(CustomFocusCallBack callBack) {
        this.mFocusCallBack = callBack;
    }

    public SkyCategoryBar(Context context) {
        super(context);
        this.mContext = context;
        this.setFocusable(true);
        this.setFocusableInTouchMode(true);                                                            // 27
        //this.setOnHoverListener(gvHoverListener);
        this.setOnKeyListener(this);
        this.setOnFocusChangeListener(this);
    }

    public void setCategoryChangeListener(OnCategoryChangeListener listener) {
        this.mCategoryChangeListener = listener;
    }

    public void setCategoryClickListener(OnCategoryClickListener listener) {
        this.mCategoryClickListener = listener;
    }

    public void setKey(boolean flag) {
        this.mKey = flag;
    }

    public void setCategoryDatas(List<SkyCategoryData> categories) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        this.mCategoryDatas = categories;
        if (mCategoryCursor == null) {
            mCategoryCursor = new SkyCategoryCursor(mContext);
            this.addView(mCategoryCursor);
        }
        if (mCategoryName == null) {
            mCategoryName = new SkyCategoryName(mContext);
            this.addView(mCategoryName);
        }
        mCategoryName.setData(categories);
        mCategoryCursor.setData(mCategoryName.getData());
        mCategoryName.setLayoutParams(params);
        mCategoryCursor.setLayoutParams(params);
        //mCategoryName.addRedPoint(4, 45);
        if (mCategoryDatas.size() > 0) {
            mCurrentName = mCategoryDatas.get(0).name;
        }
    }

    public void setFocusPos(int pos) {
        if (mCategoryDatas == null || mCategoryDatas.size() == 0)
            return;
        if (pos < 0)
            pos = 0;
        else if (pos >= mCategoryDatas.size())
            pos = mCategoryDatas.size() - 1;
        this.mCurrentPos = pos;
        mCategoryName.setFocusPos(pos);
        mCategoryCursor.setFocusPos(pos);
        if (mCategoryDatas.size() > 0) {
            mCurrentName = mCategoryDatas.get(0).name;
        }
    }

    public static String getCurrentModulName() {
        return mCurrentName;
    }

    public void addRedPoint(int index, int number) {
        mCategoryName.addRedPoint(index, number);
    }

    public void clearRedPoint() {
        mCategoryName.clearRedPoint();
    }

    public void onDestory() {
        if (mCategoryCursor != null) {
            mCategoryCursor.onDestory();
        }
        if (mCategoryName != null) {
            mCategoryName.destory();
        }
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (!mKey) {
            return true;
        }
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            mFocus = true;
            if (mCategoryCursor != null) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_DPAD_LEFT:
                        if (mCategoryCursor.setNextLeftPos()) {
                            callbackCategory(keyCode);
                        }
                        return true;
                    case KeyEvent.KEYCODE_DPAD_RIGHT:
                        if (mCategoryCursor.setNextRightPos()) {
                            callbackCategory(keyCode);
                            return true;
                        } else {
                            return false;
                        }
                    case KeyEvent.KEYCODE_DPAD_CENTER:
                    case KeyEvent.KEYCODE_ENTER:
                        if (mCategoryClickListener != null) {
                            mClickedPos = mCategoryCursor.getFocusPos();
                            mCategoryClickListener.categoryClicked(mCategoryDatas.get(mClickedPos), mClickedPos);
                        }
                        clickCategory();
                        break;
                    case KeyEvent.KEYCODE_DPAD_DOWN:
                        if (mFocusCallBack != null) {
                            return mFocusCallBack.setFocus(KeyEvent.KEYCODE_DPAD_DOWN);
                        }
                        break;
                    default:

                }
            }
        }
        return false;
    }

    protected void clickCategory() {

    }

    public void setClickPos(int pos) {
        if (mCategoryClickListener != null) {
            mCategoryClickListener.categoryClicked(mCategoryDatas.get(pos), pos);
        }
    }

    public void callbackCategory(int keyCode) {
        if (mCategoryChangeListener != null) {
            int focusPos = mCategoryCursor.getFocusPos();
            if (focusPos < 0 || mCategoryDatas == null || focusPos >= mCategoryDatas.size())
                return;
            mCategoryChangeListener.categoryChanged(
                    mCategoryDatas.get(mCategoryCursor.getFocusPos()), keyCode);
        }
    }

    @Override
    // 2012.11.8 JHF
    public boolean onTouchEvent(MotionEvent event) {
        int what = event.getAction();

        switch (what) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_UP:
                endX = (int) event.getRawX();
                endY = (int) event.getRawY();
                int onPage = -1;
                onPage = getCategoryPage(endX, endY);
                LogUtils.d("catepage", "onpage:" + onPage + " page:" + page);
                if (onPage != page && onPage != -1) {
                    if (onPage > page) {

                    }
                    if (onPage < page) {
                    }

                }
                break;
            case MotionEvent.ACTION_MOVE:

                break;
            default:
                break;
        }
        return true;
    }

    // 2012.11.8 JHF
    public int getCategoryPage(int posX, int posY) {

        List<Category> cateList = new ArrayList<Category>();
        if (mCategoryName != null) {// 当服务器未给予分类信息时，鼠标点击无效
            cateList = mCategoryName.getData();
        }
        int mpage = -1;
        if (cateList != null) {// 从服务器取得的数据不为空时执行
            for (int i = 0; i < cateList.size(); i++) {
                if (posX > cateList.get(i).mX
                        && posX < (cateList.get(i).mX + cateList.get(i).mWidth)) {
                    mpage = i;
                    return mpage;

                }
            }
        }
        return mpage;
    }

//	// 2012.11.9 JHF
//	OnHoverListener gvHoverListener = new OnHoverListener() {
//		@Override
//		public boolean onHover(View v, MotionEvent event) {
//			int what = event.getAction();
//			switch (what) {
//			case MotionEvent.ACTION_HOVER_ENTER: // 鼠标进入view
//				break;
//			case MotionEvent.ACTION_HOVER_MOVE: // 鼠标在view上
//				break;
//			case MotionEvent.ACTION_HOVER_EXIT: // 鼠标离开view
//				break;
//			case MotionEvent.ACTION_DOWN:
//				break;
//			}
//			return false;
//		}
//	};

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (mCategoryCursor != null) {
            if (hasFocus) {
                mCategoryCursor.setSelected(false);
            } else {
                mCategoryCursor.setSelected(true);
            }
        }
    }

    public SkyCategoryData getCurrentFocusData() {
        return mCategoryDatas.get(mCategoryCursor.getFocusPos());
    }

    public int getFocusPos() {
        return mCurrentPos;
    }
}
